package com.quest.space;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.ui.JBColor;
import com.quest.space.data.*;
import com.quest.space.data.enums.EventEnum;
import com.quest.space.handler.EventHandler;
import com.quest.space.handler.PokerHandler;
import com.quest.space.poker.dto.TableDto;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import static com.quest.space.handler.EventHandler.exitEvent;
import static com.quest.space.handler.EventHandler.passEvent;
import static com.quest.space.handler.UserHandler.showMenu;

public class MainWindow {
    public static MainContext mainContext;
    private JPanel jcontent;
    private JButton userBtn;
    private JTextArea textArea1;
    private JTextArea textArea2;
    private JTextArea sysArea;
    private JTextArea textArea3;
    private JTextArea textArea4;
    private JButton button1;
    private JLabel userLabe1;
    private JLabel userLabe2;
    private JButton button3;
    private JButton button2;

    public MainWindow(Project project, ToolWindow toolWindow){
        mainContext = new MainContext(textArea1, textArea2, textArea3, textArea4, sysArea, userLabe1, userLabe2);
        mainContext.setButton3(button3);
        mainContext.setButton1(button1);
        mainContext.setButton2(button2);
        //初始化界面
        sysArea.setLineWrap(true);
        Border border = BorderFactory.createLineBorder(JBColor.BLACK);
        CompoundBorder compoundBorder = BorderFactory.createCompoundBorder(border,BorderFactory.createEmptyBorder(1, 1, 1, 1));
        sysArea.setBorder(compoundBorder);
        textArea3.setBorder(compoundBorder);
        textArea4.setBorder(compoundBorder);
        button3.setVisible(false);

        String token = UserCache.token;
        if (token == null || token.isEmpty()) {
            UserCache.eventEnum = EventEnum.LOGIN;
            sysArea.setText("---login---");
            userLabe1.setText("account");
            userLabe2.setText("password");
        }else {
            showMenu();
        }
        button1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                EventHandler.submitEvent();
            }
        });
        userBtn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                Messages.showInfoMessage("昵称: 张二蛋", "用户信息");

            }
        });
        button3.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                passEvent();
            }
        });
        button2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                exitEvent();
            }
        });
    }

    public JPanel getJcontent() {
        return jcontent;
    }

    public static void showMainWindow(WebSocketMsg msg) {
        TableDto table = JSON.parseObject(msg.getData().toString(), new TypeReference<TableDto>() {});
        if (table.getTableId() != null) {
            UserCache.table = table;
        }
        PokerHandler.handle(table);
    }
}
